package cn.tedu.store.admin.webapi.controller;

import cn.tedu.store.commons.pojo.admin.dto.AdminAddNewDTO;
import cn.tedu.store.commons.pojo.admin.dto.AdminLoginDTO;
import cn.tedu.store.commons.pojo.admin.vo.AdminListItemVO;
import cn.tedu.store.commons.web.JsonResult;


import cn.tedu.store.admin.service.IAdminService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "01. 用户管理模块")
@Validated
@RestController
@RequestMapping("/admins")
public class AdminController {

    @Autowired
    IAdminService adminService;

    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 50)
    @PostMapping("/login")
    public JsonResult<String> login(AdminLoginDTO adminLoginDTO){
        String jwt = adminService.login(adminLoginDTO);
        return JsonResult.ok(jwt);
    }

    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/ams/admin/add-new')")
    public JsonResult<Void> addNew(@Validated AdminAddNewDTO adminAddNewDTO){
        log.debug("开始处理添加管理员请求 参数:{}",adminAddNewDTO);
        adminService.addNew(adminAddNewDTO);
        log.debug("添加管理员成功");
        return JsonResult.ok();
    }

    @ApiOperation("删除管理员")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(value = "管理员id", name = "id", dataType = "long",required = true)
    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/ams/admin/delete')")
    public JsonResult<Void> delete(@PathVariable Long id){
        log.debug("开始处理删除管理员的请求 参数:{}",id);
        adminService.delete(id);
        log.debug("删除管理员成功!");
        return JsonResult.ok();
    }

    @ApiOperation("启用管理员")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(value = "管理员id", name = "id", dataType = "long",required = true)
    @PostMapping("/{id:[0-9]+}/enable")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    public JsonResult<Void> setEnable(@PathVariable Long id){
        log.debug("开始处理【启用管理员】的请求，参数：{}", id);
        adminService.setEnable(id);
        return JsonResult.ok();
    }

    @ApiOperation("禁用管理员")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParam(name = "id", value = "管理员id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/disable")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    public JsonResult<Void> setDisable1(@PathVariable Long id){
        log.debug("开始处理【启用管理员】的请求，参数：{}", id);
        adminService.setDisable(id);
        return JsonResult.ok();
    }

    @ApiOperation("查询管理员列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    public JsonResult<List<AdminListItemVO>> list(){
        log.debug("开始处理获取管理员列表的请求 参数:无");
        List<AdminListItemVO> list = adminService.list();
        log.debug("查询完成");
        return JsonResult.ok(list);
    }
}
